# -*- coding: utf-8 -*- 
#!/usr/bin/env python
 
import poplib
import imaplib, email
import filters

def main():
	imap=True
	username = 'nss.automatic.message@gmail.com'  
	password = 'ThisIsMyPassword' 

	keyFilt = filters.kwordFilter()

	if imap:
		M=imaplib.IMAP4_SSL('imap.gmail.com', 993)
		M.login(username,password)
		status, count = M.select('Inbox')
		for n in range(int(count[0])):
			status, data = M.fetch(str(n+1), '(RFC822)')
			msg = email.message_from_string(data[0][1])	
			print msg['From']
			if keyFilt.isSpam(msg):
				print "Attention SPAM"
			else:
				print "Not identified as SPAM"
		M.close()
		M.logout()
	else:
		mServer = poplib.POP3_SSL('pop.gmail.com')
	 	mServer.user(username)
		mServer.pass_(password)
		numMessages = len(mServer.list()[1])
		print "You have %d messages." % (numMessages)
		print "Message List:"
		#List the subject line of each message
		for mList in range(numMessages) :
		    for msg in mServer.retr(mList+1)[1]:
			if msg.startswith('Subject'):
			    print '\t' + msg
			    break
		mServer.quit()
	
	

if __name__ == "__main__":
	main()
	 

